Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | import bcrypt from 'bcryptjs' import { and, eq } from 'drizzle-orm' import { NextResponse } from 'next/server' import { db, schema } from '@/db' import { getRoomActivePlayers } from '@/lib/arcade/player-manager' import { recordRoomMemberHistory } from '@/lib/arcade/room-member-history' import { getRoomMembers } from '@/lib/arcade/room-membership' import { withAuth } from '@/lib/auth/withAuth' import { getSocketIO } from '@/lib/socket-io' import { getUserId } from '@/lib/viewer' import { getAllGameConfigs, setGameConfig } from '@/lib/arcade/game-config-helpers' import { isValidGameName } from '@/lib/arcade/validators' import type { GameName } from '@/lib/arcade/validators' /** * PATCH /api/arcade/rooms/:roomId/settings * Update room settings * * Authorization: * - gameConfig: Any room member can update * - All other settings: Host only * * Body: * - accessMode?: 'open' | 'locked' | 'retired' | 'password' | 'restricted' | 'approval-only' (host only) * - password?: string (plain text, will be hashed) (host only) * - gameName?: string | null (any game with a registered validator) (host only) * - gameConfig?: object (game-specific settings) (any member) * * Note: gameName is validated at runtime against the validator registry. * No need to update this file when adding new games! */ export const PATCH = withAuth(async (request, { params }) => { try { const { roomId } = (await params) as { roomId: string } const userId = await getUserId() const body = await request.json() console.log( '[Settings API] PATCH request received:', JSON.stringify( { roomId, body, }, null, 2 ) ) // Read current room state from database BEFORE any changes const [currentRoom] = await db .select() .from(schema.arcadeRooms) .where(eq(schema.arcadeRooms.id, roomId)) console.log( '[Settings API] Current room state in database BEFORE update:', JSON.stringify( { gameName: currentRoom?.gameName, gameConfig: currentRoom?.gameConfig, }, null, 2 ) ) // Check if user is a room member const members = await getRoomMembers(roomId) const currentMember = members.find((m) => m.userId === userId) if (!currentMember) { return NextResponse.json({ error: 'You are not in this room' }, { status: 403 }) } // Determine which settings are being changed const changingRoomSettings = !!( body.accessMode !== undefined || body.password !== undefined || body.gameName !== undefined || body.name !== undefined || body.description !== undefined ) // Only gameConfig can be changed by any member // All other settings require host privileges if (changingRoomSettings && !currentMember.isCreator) { return NextResponse.json( { error: 'Only the host can change room settings (name, access mode, game selection, etc.)', }, { status: 403 } ) } // Validate accessMode if provided const validAccessModes = [ 'open', 'locked', 'retired', 'password', 'restricted', 'approval-only', ] if (body.accessMode && !validAccessModes.includes(body.accessMode)) { return NextResponse.json({ error: 'Invalid access mode' }, { status: 400 }) } // Validate password requirements if (body.accessMode === 'password' && !body.password) { return NextResponse.json( { error: 'Password is required for password-protected rooms' }, { status: 400 } ) } // Validate gameName if provided - check against validator registry at runtime if (body.gameName !== undefined && body.gameName !== null) { if (!isValidGameName(body.gameName)) { return NextResponse.json( { error: `Invalid game name: ${body.gameName}. Game must have a registered validator.`, }, { status: 400 } ) } } // Prepare update data const updateData: Record<string, any> = {} if (body.accessMode !== undefined) { updateData.accessMode = body.accessMode } // Hash password if provided if (body.password !== undefined) { if (body.password === null || body.password === '') { updateData.password = null // Clear password updateData.displayPassword = null // Also clear display password } else { const hashedPassword = await bcrypt.hash(body.password, 10) updateData.password = hashedPassword updateData.displayPassword = body.password // Store plain text for display } } // Update game selection if provided if (body.gameName !== undefined) { updateData.gameName = body.gameName } // Handle game config updates - write to new room_game_configs table if (body.gameConfig !== undefined && body.gameConfig !== null) { // body.gameConfig is expected to be nested by game name: { matching: {...}, memory-quiz: {...} } // Extract each game's config and write to the new table for (const [gameName, config] of Object.entries(body.gameConfig)) { if (config && typeof config === 'object') { await setGameConfig(roomId, gameName as GameName, config) console.log(`[Settings API] Wrote ${gameName} config to room_game_configs table`) } } } console.log( '[Settings API] Update data to be written to database:', JSON.stringify(updateData, null, 2) ) // If game is being changed (or cleared), delete the existing arcade session // This ensures a fresh session will be created with the new game settings if (body.gameName !== undefined) { console.log(`[Settings API] Deleting existing arcade session for room ${roomId}`) await db.delete(schema.arcadeSessions).where(eq(schema.arcadeSessions.roomId, roomId)) } // Update room settings (only if there's something to update) let updatedRoom = currentRoom if (Object.keys(updateData).length > 0) { ;[updatedRoom] = await db .update(schema.arcadeRooms) .set(updateData) .where(eq(schema.arcadeRooms.id, roomId)) .returning() } // Get aggregated game configs from new table const gameConfig = await getAllGameConfigs(roomId) console.log( '[Settings API] Room state in database AFTER update:', JSON.stringify( { gameName: updatedRoom.gameName, gameConfig, }, null, 2 ) ) // Broadcast game change to all room members if (body.gameName !== undefined) { const io = await getSocketIO() if (io) { try { console.log(`[Settings API] Broadcasting game change to room ${roomId}: ${body.gameName}`) const broadcastData: { roomId: string gameName: string | null gameConfig?: Record<string, unknown> } = { roomId, gameName: body.gameName, gameConfig, // Include aggregated configs from new table } io.to(`room:${roomId}`).emit('room-game-changed', broadcastData) } catch (socketError) { console.error('[Settings API] Failed to broadcast game change:', socketError) } } } // If setting to retired, expel all non-owner members if (body.accessMode === 'retired') { const nonOwnerMembers = members.filter((m) => !m.isCreator) if (nonOwnerMembers.length > 0) { // Remove all non-owner members from the room await db.delete(schema.roomMembers).where( and( eq(schema.roomMembers.roomId, roomId), // Delete all members except the creator eq(schema.roomMembers.isCreator, false) ) ) // Record in history for each expelled member for (const member of nonOwnerMembers) { await recordRoomMemberHistory({ roomId, userId: member.userId, displayName: member.displayName, action: 'left', }) } // Broadcast updates via socket const io = await getSocketIO() if (io) { try { // Get updated member list (should only be the owner now) const updatedMembers = await getRoomMembers(roomId) const memberPlayers = await getRoomActivePlayers(roomId) // Convert memberPlayers Map to object for JSON serialization const memberPlayersObj: Record<string, any[]> = {} for (const [uid, players] of memberPlayers.entries()) { memberPlayersObj[uid] = players } // Notify each expelled member for (const member of nonOwnerMembers) { io.to(`user:${member.userId}`).emit('kicked-from-room', { roomId, kickedBy: currentMember.displayName, reason: 'Room has been retired', }) } // Notify the owner that members were expelled io.to(`room:${roomId}`).emit('member-left', { roomId, userId: nonOwnerMembers.map((m) => m.userId), members: updatedMembers, memberPlayers: memberPlayersObj, reason: 'room-retired', }) console.log( `[Settings API] Expelled ${nonOwnerMembers.length} members from retired room ${roomId}` ) } catch (socketError) { console.error('[Settings API] Failed to broadcast member expulsion:', socketError) } } } } return NextResponse.json( { room: { ...updatedRoom, gameConfig, // Include aggregated configs from new table }, }, { status: 200 } ) } catch (error: any) { console.error('Failed to update room settings:', error) return NextResponse.json({ error: 'Failed to update room settings' }, { status: 500 }) } }) |